/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * BestHyp.java
 *
 * Created on May 20, 2011, 4:50:15 PM
 */
package pt.utl.ist.isr.livetrack.tracker.gui;

import eu.anorien.mhl.Fact;
import eu.anorien.mhl.Hypothesis;
import java.awt.EventQueue;
import java.lang.reflect.InvocationTargetException;
import java.util.Comparator;
import java.util.TreeSet;
import java.util.logging.Level;
import org.apache.log4j.Logger;
import pt.utl.ist.isr.livetrack.tracker.interfaces.BestHypothesisObserver;
import pt.utl.ist.isr.livetrack.tracker.mht.EntityInFact;
import pt.utl.ist.isr.livetrack.tracker.mht.MHTracker;

/**
 *
 * @author David Miguel Antunes <davidmiguel [ at ] antunes.net>
 */
public class BestHyp extends javax.swing.JPanel implements BestHypothesisObserver {

    /** Creates new form BestHyp */
    public BestHyp(MHTracker t) {
        initComponents();
        t.registerBestHypothesisObserver(this);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        panel = new javax.swing.JPanel();

        setLayout(new javax.swing.BoxLayout(this, javax.swing.BoxLayout.PAGE_AXIS));

        panel.setLayout(new javax.swing.BoxLayout(panel, javax.swing.BoxLayout.PAGE_AXIS));
        jScrollPane1.setViewportView(panel);

        add(jScrollPane1);
    }// </editor-fold>//GEN-END:initComponents

    public void newBestHypothesis(final Hypothesis hyp) {
        try {
//            EventQueue.invokeAndWait(new Runnable() {
//
//                public void run() {
                    panel.removeAll();
                    TreeSet<EntityPanel> panels = new TreeSet<EntityPanel>(new Comparator<EntityPanel>() {

                        public int compare(EntityPanel o1, EntityPanel o2) {
                            return new Long(o1.getEntityInFact().getEntity().getId()).compareTo(new Long(o2.getEntityInFact().getEntity().getId()));
                        }
                    });
                    for (Fact f : hyp.getFacts().keySet()) {
                        EntityInFact fact = (EntityInFact) f;
                        panels.add(new EntityPanel(fact));
                    }
                    for (EntityPanel entityPanel : panels) {
                        panel.add(entityPanel);
                    }
//                }
//            });
        } catch (Exception ex) {
        }
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JPanel panel;
    // End of variables declaration//GEN-END:variables
}
